#include "NormalMapSample.h"
#include "DirectXTK\WICTextureLoader.h"
#include "DirectXTK\DDSTextureLoader.h"
#include "Mesh.h"
NormalMapSample::NormalMapSample(ID3D11Device* device, ID3D11DeviceContext* context)
	: SampleBase(device,context),radians(0.0f)
{
	m_states = std::make_unique<CommonStates>(device);
	m_effect = std::make_unique<NormalMapEffect>(device);
	m_effect->EnableDefaultLighting();

	m_plane = GeometricPrimitive::CreateSphere(context);
	m_plane->CreateInputLayout(m_effect.get(),
		m_inputLayout.ReleaseAndGetAddressOf());

	DX::ThrowIfFailed(
		CreateWICTextureFromFile(device, L"../Resources/Models/TwoSidedPlane/TwoSidedPlane_BaseColor.png", nullptr,
			m_texture.ReleaseAndGetAddressOf()));
	m_effect->SetTexture(m_texture.Get());

	DX::ThrowIfFailed(
		CreateDDSTextureFromFile(device, L"../Resources/Textures/normalMap.dds", nullptr,
			m_normalTexture.ReleaseAndGetAddressOf()));

	m_effect->SetNormalTexture(m_normalTexture.Get());

	m_world = Matrix::Identity;
	m_view = Matrix::CreateLookAt(Vector3(2.f, 1.f, -2.f),
		Vector3::Zero, Vector3::UnitY);
	m_proj = Matrix::CreatePerspectiveFieldOfView(XM_PI / 4.f,
		16.0/9.0f, 0.1f, 10.f);

	m_effect->SetView(m_view);
	m_effect->SetProjection(m_proj);
}

NormalMapSample::~NormalMapSample()
{

}

void NormalMapSample::Update(float dt)
{
	radians += dt;
	if (radians > XM_2PI)
	{
		dt = 0.0f;
	}
	m_world = Matrix::CreateRotationY(radians);
}

void NormalMapSample::Render()
{
	m_effect->SetWorld(m_world);
	m_plane->Draw(m_effect.get(), m_inputLayout.Get(), false, false, [=] {
		auto sampler = m_states->LinearWrap();
		m_context->PSSetSamplers(1, 1, &sampler);
		});
}

void NormalMapSample::GUI()
{

}

void NormalMapSample::DrawFont()
{

}
